﻿// This source is subject to Microsoft Public License (Ms-PL).
// Please see http://taskcardcreator.codeplex.com for details.
// All other rights reserved.

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Data;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace MicrosoftVisualStudioScrum10WithDescription.Converters
{

  class DescriptionConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      try
      {
        var workItem = value as WorkItem;
        if (workItem != null)
        {
          var r = workItem.Fields["Description HTML"];
          return HtmlRemoval.StripTagsRegex((string)r.Value);
        }
        return "Error: Incorrect type";
      }
      catch (Exception exception)
      {
        return string.Format("Error: {0}", exception.Message);
      }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}

/// <summary>
/// Methods to remove HTML from strings.
/// </summary>
public static class HtmlRemoval
{
  /// <summary>
  /// Remove HTML from string with Regex.
  /// </summary>
  public static string StripTagsRegex(string source)
  {
    string sourceString;
    sourceString = Regex.Replace(source, "</P>", "\r\n");
    sourceString = Regex.Replace(sourceString, "<BR>", "\r\n");
    sourceString = Regex.Replace(sourceString, "<.*?>", string.Empty);
    sourceString = Regex.Replace(sourceString, "&nbsp;", " ");
    sourceString = Regex.Replace(sourceString, "&lt;", "");
    sourceString = Regex.Replace(sourceString, "&gt;", "");
    return sourceString;
  }
}